package cn.kooki.app.duobao.ui.Fragment.duobaobonus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseFragment;
import cn.kooki.app.duobao.base.BaseRecyclerAdapter;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.Bean.Index.IndexGoodResponse;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.Bean.User.bouns.BonunsCloseEvent;
import cn.kooki.app.duobao.data.Bean.User.bouns.BounsItem;
import cn.kooki.app.duobao.data.Bean.User.bouns.BounsResponse;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.ui.Activity.Goods.GoodsDetailAcitvity;
import cn.kooki.app.duobao.ui.Activity.Goods.SearchActivity;
import cn.kooki.app.duobao.ui.Activity.Goods.SearchResultActivity;
import cn.kooki.app.duobao.ui.Adapter.BounsItemAdapter;
import cn.kooki.app.duobao.ui.Adapter.ListRecommAdapter;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import cn.kooki.app.duobao.util.PackageUtils;
import cn.kooki.app.duobao.util.SharePreferencesUtils;
import de.greenrobot.event.EventBus;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/13.
 */
public class BonusItemFragment extends BaseFragment implements View.OnClickListener {

    public final static int NOTUSE_TYPE = 1;
    public final static int USEED_TYPE = 2;
    public final static int POST_TYPE = 3;
    @Bind(R.id.blank_go_duobao)
    Button blankGoDuobao;
    @Bind(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @Bind(R.id.list_recommend)
    RecyclerView listRecommend;
    @Bind(R.id.emptyWrapper)
    ScrollView emptyWrapper;
    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loadingWrapper)
    CustomRelativeLayout loadingWrapper;

    private ArrayList<BounsItem> datas = new ArrayList<>();
    private BounsItemAdapter adapter;
    private UserInfo userinfo;
    private int type;
    private int page = 0;
    private CustomRefreshHeader refreshHeader;
    private ListRecommAdapter listRecommAdapter;

    @Override
    public View setView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.blank_duobao_bonus, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void afterView() {
        userinfo = SharePreferencesUtils.getUserinfo(context);
        Bundle arguments = getArguments();
        type = arguments.getInt("type");
        loadingWrapper.showloading();
        emptyWrapper.setVisibility(View.GONE);
        adapter = new BounsItemAdapter(context, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent(SearchResultActivity.class, "title", "全部商品");
            }
        });

        refreshHeader = new CustomRefreshHeader(context);
        swipeRefreshLayout.setHeaderView(refreshHeader);
        swipeRefreshLayout.addPtrUIHandler(refreshHeader);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }
        });
        getdata(false);
        blankGoDuobao.setOnClickListener(this);
        listRecommend.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        listRecommAdapter = new ListRecommAdapter(context);
        listRecommend.setAdapter(listRecommAdapter);
        listRecommAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                GoodsItem item = (GoodsItem) data;
                Intent intent = new Intent(context, GoodsDetailAcitvity.class);
                intent.putExtra("id", item.getId());
                intent.putExtra("qishu", item.getQishu());
                context.startActivity(intent);
            }
        });
        gettuijian();
    }


    /**
     * 获取推荐商品
     */
    private void gettuijian() {
        MainFactory.getApiInstance().tuijianlist(new Callback<IndexGoodResponse>() {
            @Override
            public void success(IndexGoodResponse indexGoodResponse, Response response) {
                if (indexGoodResponse.getData() != null) {
                    listRecommAdapter.mDatas.clear();
                    listRecommAdapter.addDatas(indexGoodResponse.getData());
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    /**
     * 获取红包数据
     *
     * @param b 是否加载更多
     */
    private void getdata(boolean b) {
        if (!b) {
            page = 0;
            MainFactory.getApiInstance().listbonus(userinfo.getUid(), type, page, "", new Callback<BounsResponse>() {
                @Override
                public void success(BounsResponse bounsResponse, Response response) {
                    if (bounsResponse.getData() != null) {
                        datas.clear();
                        datas.addAll(bounsResponse.getData());
                        refreshView();
                    }
                    hideloading();
                }


                @Override
                public void failure(RetrofitError error) {
                    hideloading();
                }
            });

        }
    }

    private void hideloading() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.refreshComplete();
            loadingWrapper.hideloading();
        }
    }

    private void refreshView() {
        adapter.notifyDataSetChanged();
        if (datas.size() > 0 && swipeRefreshLayout != null) {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyWrapper.setVisibility(View.GONE);
        } else if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setVisibility(View.GONE);
            emptyWrapper.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onClick(View v) {
        if (v == blankGoDuobao) {
            ChangePageEvent pageEvent = new ChangePageEvent();
            pageEvent.item = 0;
            EventBus.getDefault().post(pageEvent);
            EventBus.getDefault().post(new BonunsCloseEvent());
        }
    }
}
